package com.to.aboomy.pager2banner;

import ohos.agp.components.Component;
import ohos.agp.components.PageSlider;

/**
 * @author xujianhong
 * @since 2021-07-20
 */
public class ScaleInTransformer implements PageSlider.PageChangedListener {

    private static final float DEFAULT_CENTER = 0.5f;
    private float mMinScale = 0.85f;
    private Component mView;
    public ScaleInTransformer(Component view){
        this.mView = view;
    }

    @Override
    public void onPageSliding(int itemPos, float itemPosOffset, int itemPosOffsetPixels) {
        int pageWidth = mView.getWidth();
        int pageHeight = mView.getHeight();
        mView.setPivotY(pageHeight >> 1);
        mView.setPivotX(pageWidth >> 1);
        if (itemPosOffset < -1) { // [-Infinity,-1)
            // This page is way off-screen to the left.
            mView.setScaleX(mMinScale);
            mView.setScaleY(mMinScale);
            mView.setPivotX(pageWidth);
        } else if (itemPosOffset <= 1) {
            // [-1,1]
            // Modify the default slide transition to shrink the page as well
            if (itemPosOffset < 0) {
                //1-2:1[0,-1] ;2-1:1[-1,0]
                float scaleFactor = (1 + itemPosOffset) * (1 - mMinScale) + mMinScale;
                mView.setScaleX(scaleFactor);
                mView.setScaleY(scaleFactor);
                mView.setPivotX(pageWidth * (DEFAULT_CENTER + (DEFAULT_CENTER * -itemPosOffset)));
            } else {
                //1-2:2[1,0] ;2-1:2[0,1]
                float scaleFactor = (1 - itemPosOffset) * (1 - mMinScale) + mMinScale;
                mView.setScaleX(scaleFactor);
                mView.setScaleY(scaleFactor);
                mView.setPivotX(pageWidth * ((1 - itemPosOffset) * DEFAULT_CENTER));
            }
        } else {
            // (1,+Infinity]
            mView.setPivotX(0);
            mView.setScaleX(mMinScale);
            mView.setScaleY(mMinScale);
        }
    }

    @Override
    public void onPageSlideStateChanged(int i) {

    }

    @Override
    public void onPageChosen(int i) {

    }
}
